package com.tg.app.adapter;

import android.net.wifi.ScanResult;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.R;


import java.util.List;

public class ApDeviceAdapter extends RecyclerView.Adapter<ApDeviceAdapter.ApDeviceViewHodler> {
    private List<ScanResult> list;
    private OnAddWifiDeviceClickListener listener;


    public ApDeviceAdapter(List<ScanResult> list, OnAddWifiDeviceClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ApDeviceViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.device_add_ap_item, parent,false);
        return new ApDeviceViewHodler(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ApDeviceViewHodler holder,final int position) {
        ScanResult  sWifiAp = list.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
        holder.deviceWifiName.setText(sWifiAp.SSID.replace("\"",""));
        holder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public interface OnAddWifiDeviceClickListener {
        void onItemClick(int position);
    }

    public class ApDeviceViewHodler extends RecyclerView.ViewHolder {
        TextView deviceWifiName;
        Button btnAdd;
        public ApDeviceViewHodler(View view) {
            super(view);
            deviceWifiName = view.findViewById(R.id.device_add_ap_camera_name);
           btnAdd = view.findViewById(R.id.device_add_ap_select);
        }
    }
}
